<?php
namespace hangpu8\hpcms\service;

use hangpu8\hpcms\model\AdminRole as ModelAdminRole;
use think\Exception;

class AdminRole
{
    private $db;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->db = new ModelAdminRole;
    }

    /**
     * 获取列表数据
     *
     * @param array $where
     * @param string $order
     * @param string $field
     * @param integer $limit
     * @return array
     */
    public function getlist($where = [],$order = 'id asc',$field = '*',$limit = 300) : array
    {
        $system = ['正常','系统'];
        $map = [];
        if ($where)
        {
            $map = $where;
        }
        $list = $this->db->where($map)
        ->order($order)
        ->field($field)
        ->paginate($limit)
        ->each(function($item)use($system){
            if (isset($item['is_system']))
            {
                $item['is_system_text'] = $system[$item['is_system']];
            }
            return $item;
        })->toArray();
        return $list;
    }
    
    /**
     * 创建数据
     *
     * @param array $post
     * @return bool
     */
    public function create(array $post) : bool
    {
        if ($this->db->save($post))
        {
            return true;
        }else{
            throw new Exception("添加数据失败",404);
        }
    }
    
    /**
     * 修改数据
     *
     * @param array $post
     * @param array $map
     * @return boolean
     */
    public function saveUpdate(array $post,array $map) : bool
    {
        try {
            return $this->db->where($map)->save($post);
        } catch (\Throwable $e) {
            throw new Exception($e->getMessage(),404);
        }
    }

    /**
     * 删除数据
     *
     * @param [type] $id
     * @return void
     */
    public function del($id)
    {
        try {
            $adminRole = ModelAdminRole::where(['id'=> $id])->find();
            if ($adminRole->is_system)
            {
                throw new Exception("该角色不允许删除",404);
            }
            // 软删除
            return $adminRole->delete();
        } catch (\Throwable $e) {
            throw new Exception($e->getMessage(),404);
        }
    }

    /**
     * 获取表单数据
     *
     * @param array $where
     * @return array
     */
    public function getForm(array $where) : array
    {
        $data = $this->db->where($where)->find();
        if ($data)
        {
            $data = $data->toArray();
        }
        return $data;
    }

    /**
     * 获取权限列表
     *
     * @param [type] $rule
     * @return void
     */
    public function getAuthRule($rule)
    {
        $list = [];
        $i = 0;
        foreach ($rule as $value)
        {
            $list[$i]['title'] = $value['title'];
            $list[$i]['id'] = $value['path'];
            if ($value['_data'])
            {
                $list[$i]['children'] = $this->getAuthRule($value['_data']);
            }
            $i++;
        }
        return $list;
    }

    /**
     * 获取option数据
     *
     * @return void
     */
    public function getRoleOptions()
    {
        $list = $this->getlist([],'id asc','id,title');
        $data = [];
        foreach ($list['data'] as $key => $value)
        {
            $data[$key]['label'] = $value['title'];
            $data[$key]['value'] = $value['id'];
        }
        return $data;
    }

    /**
     * 查找父级规则
     *
     * @param [type] $rule
     * @return void
     */
    public function getParents($rules) : array
    {
        $model = new \hangpu8\hpcms\model\AuthRule;
        $map[] = ['path','in',$rules];
        $list = $model->where($map)->column('pid');
        $parentList = [];
        foreach ($list as $path)
        {
            $parentList[$path] = $path;
        }
        $parentList = array_values($parentList);
        if ($parentList)
        {
            $rules = array_merge($rules,$this->getParents($parentList));
        }
        return $rules;
    }
}